home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CSpinCursor / CSpinCursor.cp < prev    next >
Encoding:
Text File  |  1994-11-30  |  5.3 KB  |  163 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CSpinCursor.cp
  3.     
  4.     Implements a spinning color cursor. A list of 'crsr' cursors is specified 
  5.     with an'acur' resource. Repeatedly invoking the Spin() method sequences
  6.     through the list of cursors.
  7.     
  8.     SUPERCLASS = CObject.c
  9.     
  10.     Copyright © 1993 Martin R. Wachter. All rights reserved.
  11.     Based on some code I found from GHD on AOL a few years ago.
  12.     
  13. ******************************************************************************/
  14.  
  15. #include "CSpinCursor.h"
  16.  
  17. typedef    struct    acurCURS    {            /* For each cursor in an 'acur'        */
  18.     short    resID;                        /*   CURS resource ID                */
  19.     short    padding;                    /*   Two bytes of padding            */
  20. } acurCURS;
  21.  
  22. typedef    struct    acurTemplate    {        /* Resource template for 'acur'        */
  23.     short        numCursors;                /*   Number of cursors in list        */
  24.     short        counter;                /*   Counter (not used by us)        */
  25.     acurCURS    CURSlist[8];                /*   'CURS' ID/padding pairs        */
  26. } acurTemplate, **acurTemplateH;
  27.  
  28.  
  29. /******************************************************************************
  30. ISpinCursor
  31.  
  32.     Initialize a Spinner object
  33. ******************************************************************************/
  34.  
  35. void    CSpinCursor::ISpinCursor(
  36.     short    acurID,                        /* 'acur' resource ID                */
  37.     short    aThreshold,                    /* Tick delay before first spin        */
  38.     short    anInterval)                    /* Ticks between spins                */
  39. {
  40.     register acurTemplateH    tempH;        /* 'acur' handle with entries        */
  41.                                         /*   as ID & padding pairs            */
  42.     register short            i;            /* Loop counter                        */
  43.     register CCrsrHandle        aCursor;/* Handle to 'CURS' resource        */
  44.     
  45.                                         /* Cursor IDs are in an 'acur'        */
  46.                                         /*   resource                        */
  47.     tempH = (acurTemplateH) GetResource('acur', acurID);
  48.     
  49.     DetachResource((Handle)tempH);                /* We are going to change the        */
  50.     HNoPurge((Handle)tempH);                    /*   'acur' resource in memory.        */
  51.                                         /*   Treat it as a regular handle    */
  52.                                         /*   and make it nonpurgeable.        */
  53.     
  54.         /* Cursor IDs list entries in the 'acur' are 4 bytes long.    */
  55.         /* The first 2 bytes are a 'CURS' resource ID and the        */
  56.         /* last 2 bytes are padding. This is so that the ID/padding    */
  57.         /* entry can be replaced with a handle (4 bytes long) to    */
  58.         /* the corresponding 'CURS' resource at runtime. "tempH"    */
  59.         /* and "acurH" both refer to the same handle. "tempH"        */
  60.         /* treats the entries as ID/padding pairs, whereas "acurH"    */
  61.         /* treats the entries as cursor handles.                    */
  62.         
  63.     acurH = (acurHand) tempH;
  64.     
  65.     for (i = 0; i < (**tempH).numCursors; i++) {
  66.                                         /* Get handle to Cursor                */
  67.         aCursor = GetCCursor((**tempH).CURSlist[i].resID);
  68.         HNoPurge((Handle)aCursor);                /* Make it nonpurgeable                */
  69.         
  70.                                         /* Replace 'CURS' ID/padding pairs    */
  71.                                         /*   with a handle to the Cursor    */
  72.         (**acurH).cursors[i] = aCursor;
  73.     }
  74.     
  75.     SetTimes(aThreshold, anInterval);    /* Specify delay times                */
  76.     Reset();                            /* Initialize counters                */
  77. }
  78.  
  79.  
  80. /******************************************************************************
  81. Dispose
  82.  
  83.     Dispose of a Spinner
  84. ******************************************************************************/
  85.  
  86. void    CSpinCursor::Dispose()
  87. {
  88. short    i;
  89.  
  90.     for (i = 0; i < (**acurH).numCursors; i++) {
  91.         DisposCCursor((**acurH).cursors[i]);
  92.     }
  93.  
  94.     DisposHandle((Handle)acurH);        /* Throw out our modified copy        */
  95.                                         /*   of the 'acur' resource            */
  96.     inherited::Dispose();
  97. }
  98.  
  99.  
  100. /******************************************************************************
  101. Reset
  102.  
  103.     Reset counters to initial values. Subsequent spins will start with
  104.     the first cursor and will occur after the "threshold" delay.
  105. ******************************************************************************/
  106.  
  107. void    CSpinCursor::Reset()
  108. {
  109.     cursIndex = 0;                        /* Start with the first cursor        */
  110.     nextSpinTime = 0;                    /* Indicates first spin is next        */
  111. }
  112.  
  113.  
  114. /******************************************************************************
  115. SetTimes
  116.  
  117.     Specifiy values for the threshold delay before the first spin
  118.     and the interval between spins
  119. ******************************************************************************/
  120.  
  121. void    CSpinCursor::SetTimes(
  122.     short    newThreshold,
  123.     short    newInterval)
  124. {
  125.     threshold = newThreshold;            /* Set instance variables            */
  126.     interval = newInterval;
  127. }
  128.  
  129.  
  130. /******************************************************************************
  131. Spin
  132.  
  133.     Spin the cursor. Send this message repeatedly during long delays
  134.     to spin the cursor. Cursor spinning is accomplished by changing
  135.     the cursor after a specified time interval.
  136. ******************************************************************************/
  137.  
  138. void    CSpinCursor::Spin()
  139. {
  140.     register long    theTickCount;
  141.     
  142.     theTickCount = TickCount();            /* Time in ticks since startup        */
  143.     
  144.     if (nextSpinTime == 0) {            /* Cursor has not yet spun            */
  145.                                         /* Set time counter so that the        */
  146.                                         /*   first spin will occur after    */
  147.                                         /*   "threshold" ticks                */
  148.         nextSpinTime = theTickCount + threshold;
  149.     
  150.     } else if (theTickCount >= nextSpinTime) {
  151.                                         /* Enough time has passed. Change    */
  152.                                         /*   the cursor                        */
  153.         SetCCursor((**acurH).cursors[cursIndex]);
  154.         
  155.         cursIndex++;                    /* Increment cursor index            */
  156.         if (cursIndex >= (**acurH).numCursors) {
  157.             cursIndex = 0;                /* Wrap around to the first one        */
  158.         }
  159.                                         /* Next spin will occur after at    */
  160.                                         /*   least "interval" ticks            */
  161.         nextSpinTime = theTickCount + interval;
  162.     }
  163. }